package com.shop2cn.iapi.agentproducts.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.CatalogProperty;
import com.shop2cn.iapi.agentproducts.parameter.GetCatalogPropertyListBean;
import com.shop2cn.iapi.agentproducts.service.GetCatalogPropertyListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.AgentUsers;

/**
 * 获取规格属性列表 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetCatalogPropertyList {
	private static GetCatalogPropertyListBean getcatalogpropertylistBean;
	private static GetCatalogPropertyListCall getcatalogpropertylistCall;
	private static int sellerId = AgentUsers.rootUserId;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取规格属性列表");
	}

	@Before
	public void caseUp() {
		getcatalogpropertylistBean = new GetCatalogPropertyListBean();
		getcatalogpropertylistCall = new GetCatalogPropertyListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getcatalogpropertylistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDB(GetCatalogPropertyListBean getcatalogpropertylistBean) {
		YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
		List<CatalogProperty> catalogPropertyList = getcatalogpropertylistCall.getCatalogPropertyList();
		catalogPropertyList.forEach(p -> {
			ymtProductsIWapper.selectUserCatalogProperty(p.getName(), null).forEach(x -> {
				if (Integer.parseInt(x.get("UserId").toString()) == 0) {
					Logger.verifyEquals(true, p.isPreset(), "验证preset");
				} else {
					Logger.verifyEquals(false, p.isPreset(), "验证preset");
				}
				Logger.verifyEquals(true, Integer.parseInt(x.get("UserId").toString()) == sellerId
						|| Integer.parseInt(x.get("UserId").toString()) == 0, "验证返回数据");
			});

		});
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetCatalogPropertyList_01() {
		Logger.start(true, "获取规格属性列表");
		try {
			getcatalogpropertylistBean.setUserId(sellerId);
			getcatalogpropertylistCall.setData(getcatalogpropertylistBean);
			getcatalogpropertylistCall.callService();
			Logger.verifyEquals("200", getcatalogpropertylistCall.getString("code"), "验证code");
			// verifyDB(getcatalogpropertylistBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetCatalogPropertyList_02() {
		Logger.start(true, "sellerId为空，失败");
		try {
			getcatalogpropertylistBean.setUserId(0);
			getcatalogpropertylistCall.setData(getcatalogpropertylistBean);
			getcatalogpropertylistCall.callService();
			Logger.verifyEquals("400", getcatalogpropertylistCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetCatalogPropertyList_03() {
		Logger.start(true, "传任意userid，返回系统默认规格属性");
		try {
			getcatalogpropertylistBean.setUserId(66666);
			getcatalogpropertylistCall.setData(getcatalogpropertylistBean);
			getcatalogpropertylistCall.callService();
			Logger.verifyEquals("500", getcatalogpropertylistCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}